﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP.Styles
{
    public partial class productupload : System.Web.UI.Page
    {        
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "/uploads/products/";

            if (FileUpload1.HasFile)
            {
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(uploadFolder + now + extension);
                UploadStatusLabel.Text = "File uploaded successfully as: " ;
                imgName.Text = now + extension;
            }  
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdInsert;
                conn.Open();

                cmdInsert = new SqlCommand("INSERT INTO Products VALUES (@name, @desc, @quan, @price, @pic, @cat, @uid, @pviews, @pstatus)", conn);
                cmdInsert.Parameters.AddWithValue("@name", txtPname.Text);
                cmdInsert.Parameters.AddWithValue("@desc", txtPdesc.Text);
                cmdInsert.Parameters.AddWithValue("@quan", txtStock.Text);
                cmdInsert.Parameters.AddWithValue("@price", txtPrice.Text);
                cmdInsert.Parameters.AddWithValue("@pic", imgName.Text);
                cmdInsert.Parameters.AddWithValue("@cat", ddlCat.Text);
                cmdInsert.Parameters.AddWithValue("@uid", "1");
                cmdInsert.Parameters.AddWithValue("@pviews", "0");
                cmdInsert.Parameters.AddWithValue("@pstatus", "Pending");
                int success = cmdInsert.ExecuteNonQuery();

                if (success == 1)
                {
                    MessageBox("Upload Successfully!");
                    Redirect("ProductsList.aspx");
                }

                conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

    }
}